<template>
	<view class="container">
		<u-toast ref="uToast" />
		<u-navbar :is-back="true" title="上传文件" back-icon-color='#333333' height="50" :border-bottom="false">
		</u-navbar>
		<u-select v-model="show" mode="single-column" :list="list" @confirm="confirm"></u-select>
		<view class="list">
			<view class="choose_location">
				<text>{{location}}</text>
				<text>{{content}}</text>
				<text @click="show=true"></text>
			</view>
			<view class="choose_file">
				<text>{{file}}</text>
				<text @click.stop="openFile">{{unselect}}</text>
			</view>
		</view>
		<view class="confirm">
			<view class="verify" @click.stop="uploadMyFile">
				{{verify}}
			</view>
		</view>
	</view>
</template>

<script>
	import request from '../../request.js'
	export default {
		data() {
			return {
				location: '选择位置',
				content: '',
				file: '选择文件',
				unselect: '去选择',
				verify: "确认",
				show: false,
				list: [],
				file_value: '',
				uploadFile: [],
				form: {
					parent_folder_id: "",
					disk_type: "",
					ext: '',
					name: '',
					file_type: '',
					file_id: "",
				},

			}
		},
		onLoad() {
			for (let i = 1; i < 6; i++) {
				this.getFileTree(i);
			}

		},
		methods: {
			getFileTree(param) {
				let base_url = uni.getStorageSync('host');
				console.log(base_url);
				let _this = this;
				request({
					url: base_url + 'teacher/net_disk/getFileTree',
					method: 'post',
					data: {
						disk_type: param,
					},
					success(res) {
						if (res.statusCode == 200) {
							let obj = res.data.data;
							if (obj.length > 0) {
								_this.list.push({
									label: '+' + obj[0].name,
									value: `${obj[0].parent_id},${obj[0].disk_type}`,
								})
								if (obj[0].next_level.length > 0) {
									for (let i = 0; i < obj[0].next_level.length; i++) {
										console.log('触发了吗')
										_this.list.push({
											label: '-' + obj[0].next_level[i].name,
											value: `${obj[0].next_level[i].parent_id},${obj[0].next_level[i].disk_type}`,
										})
										if (obj[0].next_level[i].next_level.length > 0) {
											for (let j = 0; j < obj[0].next_level[i].next_level.length; j++) {
												console.log('触发了吗')
												_this.list.push({
													label: '--' + obj[0].next_level[i].next_level[j].name,
													value: `${obj[0].next_level[i].next_level[j].parent_id},${obj[0].next_level[i].next_level[j].disk_type}`,
												})
												if (obj[0].next_level[i].next_level[j].next_level.length > 0) {
													for (let k = 0; k < obj[0].next_level[i].next_level[j]
														.next_level.length; k++) {
														console.log('触发了吗')
														_this.list.push({
															label: '---' + obj[0].next_level[i].next_level[
																j].next_level[k].name,
															value: `${obj[0].next_level[i].next_level[j].next_level[k].parent_id},${obj[0].next_level[i].next_level[j].next_level[k].disk_type}`,
														})
													}

												}


											}

										}

									}

								}
							}
						} else {
							_this.$refs.uToast.show({
								title: res.data.message || '系统错误',
								type: 'warning',
							})
						}
					}
				})
			},
			//选择文件
			openFile() {
				let base_url = uni.getStorageSync('host');
				let _this = this;
				uni.chooseFile({
					count: 6, //默认9
					extension: ['.zip', '.doc'],
					success: function(res) {
						console.log(JSON.stringify(res.tempFilePaths));
						const uploadTask = uni.uploadFile({
							formData: {
								files: res.tempFilePaths[0],
							},
							url: base_url + 'teacher/net_disk/uploadTemp',
							filePath: res.tempFilePaths[0],
							name: 'files',
							success: function(res) {
								let list = JSON.parse(res.data);
								console.log(list.data);
								_this.form.file_id = list.data.file_id;
								_this.form.file_type = list.data.file_type;
								_this.form.name = list.data.name;
								_this.form.ext = list.data.ext;

							}
						});
					}
				});
			},
			//上传文件
			uploadMyFile() {
				let base_url = uni.getStorageSync('host');
				console.log(base_url);
				let arr = [];
				let _this = this;
				arr = this.file_value ? this.file_value.split(',') : [];
				this.form.parent_folder_id = arr[0];
				this.form.disk_type = arr[1];
				request({
					url: base_url + 'teacher/net_disk/uploadMyFile',
					method: 'post',
					data: {
						...this.form,
					},
					success(res) {
						if (res.statusCode == 200) {
							console.log(res.data);
							_this.$toLink('../file_management/file_management', {})
						} else {
							_this.$refs.uToast.show({
								title: res.data.message || '系统错误',
								type: 'warning',
							})
						}

					}
				})
			},
			confirm(e) {
				console.log(e, e[0].value);
				this.content = e[0].label;
				this.file_value = e[0].value;
			}
		}
	}
</script>

<style lang="scss">
	.container {
		width: 100%;
		height: 100vh;
		background-color: #F0F0F0;

		.list {
			width: 710rpx;
			margin-top: 20rpx;
			background: #fff;
			margin-left: 20rpx;
			border-radius: 12rpx;

			.choose_location {
				width: 94%;
				height: 85rpx;
				margin-left: 3%;
				color: #333333;
				font-size: 30rpx;
				border-bottom: 1px solid #F2F2F2;
				display: flex;
				justify-content: flex-start;
				align-items: center;

				text {
					&:nth-child(2) {
						width: 470rpx;
						text-align: right;
					}

					&:nth-child(3) {
						width: 0rpx;
						margin-left: 20rpx;
						margin-top: 20rpx;
						height: 0rpx;
						border-width: 20rpx 20rpx 20rpx 20rpx;
						border-color: #ccc transparent transparent transparent;
						border-style: solid;
					}
				}

			}

			.choose_file {
				width: 94%;
				height: 85rpx;
				margin-left: 3%;
				color: #333333;
				font-size: 30rpx;
				display: flex;
				justify-content: flex-start;
				align-items: center;

				text {
					&:nth-child(1) {}

					&:nth-child(2) {
						width: 150rpx;
						height: 50rpx;
						line-height: 50rpx;
						text-align: center;
						margin-left: 390rpx;
						background-color: #EDF4FB;
						border-radius: 2rpx;
						border: 1rpx solid #F2F2F2;
						font-size: 24rpx;

					}
				}

			}
		}

		.confirm {
			width: 100%;
			margin-top: 400rpx;
			height: 300rpx;
			display: flex;
			justify-content: center;
			align-items: center;

			.verify {
				width: 200rpx;
				height: 60rpx;
				border-radius: 50rpx;
				text-align: center;
				line-height: 60rpx;
				background: linear-gradient(to right, #2C78FE, #32B0FF);
				color: #fff;
				font-size: 30rpx;
			}
		}


	}
</style>
